package com.lysj.bhp.withdraw.controller;

import com.lysj.bhp.manager.domain.Role;
import com.lysj.bhp.manager.domain.Staff;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.withdraw.base.WithdrawContent;
import com.lysj.bhp.withdraw.domain.Poundage;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("admin/poundage")
public class PoundageController extends WithdrawContent {


    @GetMapping("list")
    public Resp list(Poundage model) {
        model.setIsExist(1);
        Specification specification = ParamUtil.createSpecification(model);
        List<Poundage> poundageList = poundageRepository.findAll(specification,new Sort(Sort.Direction.ASC, "poundageId"));
        return new Resp(poundageList);
    }

    @PostMapping("/add")
    public Resp add(Poundage model) {
        if (ParamUtil.isBlack(model.getCoinName())) {
            return new Resp(Resp.PARAM_ERROR, "币种名称不能为空");
        }
        if (BigDecimal.ZERO.equals(model.getMiniNumber()) || null == model.getMiniNumber()) {
            return new Resp(Resp.PARAM_ERROR, "最小提币数量不能为空或者0");
        }
        if (BigDecimal.ZERO.equals(model.getMaxNumber()) || null == model.getMaxNumber()) {
            return new Resp(Resp.PARAM_ERROR, "最大提币数量不能为空或者0");
        }
        if (BigDecimal.ZERO.equals(model.getTransferFee()) || null == model.getTransferFee()) {
            return new Resp(Resp.PARAM_ERROR, "转出手续费不能为空或者0");
        }
        if (ParamUtil.isBlack(model.getType())) {
            return new Resp(Resp.PARAM_ERROR, "请选择币种类型");
        }
        Poundage poundage = poundageRepository.findByType(model.getType());
        if (poundage != null) {
            return new Resp(Resp.PARAM_ERROR, "该币种类型已存在");
        }
        model.setIsExist(1); //设置为正常状态
        poundageRepository.save(model);
        return new Resp("添加成功");
    }

    @PostMapping("/edit")
    public Resp edit(Poundage model) {
        if (ParamUtil.isBlack(model.getPoundageId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择币种");
        }
        Poundage poundage = poundageRepository.findOne(model.getPoundageId());
        if (null == poundage) {
            return new Resp(Resp.PARAM_ERROR, "查无此币种");
        }
        if (ParamUtil.isBlack(model.getCoinName())) {
            return new Resp(Resp.PARAM_ERROR, "币种名称不能为空");
        }
        if (BigDecimal.ZERO.equals(model.getMiniNumber()) || null == model.getMiniNumber()) {
            return new Resp(Resp.PARAM_ERROR, "最小提币数量不能为空或者0");
        }
        if (BigDecimal.ZERO.equals(model.getMaxNumber()) || null == model.getMaxNumber()) {
            return new Resp(Resp.PARAM_ERROR, "最大提币数量不能为空或者0");
        }
        if (BigDecimal.ZERO.equals(model.getTransferFee()) || null == model.getTransferFee()) {
            return new Resp(Resp.PARAM_ERROR, "转出手续费不能为空或者0");
        }
        poundage.setCoinName(model.getCoinName());
        poundage.setMiniNumber(new BigDecimal(""+model.getMiniNumber()));
        poundage.setMaxNumber(new BigDecimal(""+model.getMaxNumber()));
        poundage.setTransferFee(new BigDecimal(""+model.getTransferFee()));
        poundage.setExchangeRate(new BigDecimal(""+model.getExchangeRate()));
        poundageRepository.save(poundage);
        return new Resp("修改成功");
    }

    @PostMapping("remove")
    public Resp remove(Poundage model) {
        if (ParamUtil.isBlack(model.getPoundageId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择手续费");
        }
        Poundage poundage = poundageRepository.findOne(model.getPoundageId());
        if (poundage == null) {
            return new Resp(Resp.PARAM_ERROR, "查无此手续费!");
        }
        poundage.setIsExist(2);//逻辑删除
        poundageRepository.save(poundage);
        return new Resp("删除成功");
    }

}
